// pages/cultrueConpous/index.js
const common = require('../../public.js')
const utils = require('../../utils/utils.js')
const sdCityConfig = require('./sdCityList')
Page({
  data: {
    xpoint:'',
    ypoint:'',
    sdCitys: sdCityConfig.cityLocalJson,
    activeRegion: -1,
    dcId: '',
    pageSize: 10,
    pageNum: 1,
    // parames: '',
    loadMore: true, //可以加载更多
    discountCouponList: [],
    discountTypeList: [],
    adminDiscountCouponList: '',
    adminDiscountTypeList: '',
    citys: [],
    activeCity: -1, // 选中城市
    districtList: [],
    totalCouponsList: [],
    imageId: 'default', // 标记当前城市id，用于处理上方的广告图片
    isLoadingBusy: false,
    showFullLoading: false,
    regionId: '', //地区id 
    flag:false,
    cityes:"",
    managerId:""
  },
  onLoad: function (options) { 
  console.log(options,"跳转")
//  var managerId = options.managerId ? options.managerId :""
//  this.setData({managerId})
  options.acts ?  this.setData({flag:true,}) : this.setData({flag:false})
//  区级公众号
if(options.id){
  this.setData({
    activeRegion:options.id,
    regionId:options.regionId
  })
 
 
}
 
  },
  onShow: function () {
   var that = this
    if (this.data.citys.length === 0) {
      // 首次加载页面，此时没有城市列表
      getApp().getLocation(function (data) {
        const currentCity = (data && data.regeocodeData && data.regeocodeData.addressComponent) ?
          data.regeocodeData.addressComponent.city : null
        that.getCityList(currentCity)   
        console.log("wudingwei")
      }, function () {
        // 定位失败
      
        that.getCityList(null)
      })
    } else {
      // 非首次加载（返回到当前页面），刷新当前城市数据
      that.reloadCouponData()
    }
  },
  getCityList(currentCity) { // 获得要查询地市
    let current = -1
    if (currentCity) {
      for (let j = 0, len = this.data.sdCitys.length; j < len; j++) {
        const item = this.data.sdCitys[j]
        if (item.l == currentCity) {
          current = j
          break
        }
      }
    }


    this.setData({
      citys: this.data.sdCitys,
      activeCity: current
    })
    if(this.data.flag){
      this.setData({
        activeCity: -1
      })
      this.getProvinceCoupons()
      return
    }
    this.changeImageId() // 修改imageId，更新上方广告图片
    this.getAllDisTrict() // 获取所有的区县列表 地级市
    if(this.data. activeRegion!=-1){
      this.getDisTrictCoupons(Number(this.data.regionId))
      return
    }
    if (current >= 0) {
      this.getCityCoupons(this.data.sdCitys[current].v) // 获取市券，默认区县选择时当前城市
    } else {
      // 山东省券
      this.getProvinceCoupons()
    }
  },
  getAllDisTrict() {
    if (this.data.activeCity !== -1) {
      // -1为未获取到当前城市
      const districtList = this.data.sdCitys[this.data.activeCity].c
      this.setData({
        districtList
      })
    }
    // let tempJson = {
    //   parentId: id
    // }
    // const that = this
    // common.post('/region/getRegionByParentID', tempJson, function (res) {
    //   if (res.status === 200) {
    //     console.log('获取区县', res)
    //     that.setData({
    //       districtList: res.data
    //     })
    //   } else {
    //     common.showRequestErrMsg(res)
    //   }
    // })
  },
  getProvinceCouponsAction() {
    if (this.data.activeCity == -1) return
  
    this.getProvinceCoupons()
  },
  getProvinceCoupons() { // 获得省券
    this.setData({
      activeCity: -1,
      districtList: [],
      discountCouponList: [],
      adminDiscountCouponList: [],
      isLoadingBusy: true,
      regionId: '' // 省券没有地区id
    })
    this.changeImageId()
    const that = this
    const tempJson = {
      provinceId: 16
    }
    common.post('/cityDisCoupon/getCityDisCouponListByProvinceId', tempJson, function (res) {
      if (res.status === 200) {
        that.setData({
          discountCouponList: res.data
        })
      } else {
        common.showRequestErrMsg(res)
      }
    }, function () {
      that.setData({
        isLoadingBusy: false
      })
    })
  },
  getCityCoupons(id) { //获得市券
    this.setData({
      isLoadingBusy: true,
      discountCouponList: [],
      adminDiscountCouponList: []
    })
    let params = {
      cityId: id
    }
    const that = this
    common.post('/cityDisCoupon/getCityDisCouponListByCityId', params, function (res) {
      if (res.status === 200) {
        that.setData({
          // totalCouponsList:res.data
          discountCouponList: res.data.discountCouponList,
          adminDiscountCouponList: res.data.adminDiscountCouponList
        })
      } else {
        that.setData({
          // totalCouponsList:res.data
          discountCouponList: [],
          adminDiscountCouponList: []
        })
        common.showRequestErrMsg(res)
      }
    },
      function () {
        that.setData({
          isLoadingBusy: false
        })
      })
  },
  getDisTrictCoupons(id) { //获得地区券
    const tempJson = {
      regionId: id
    }
    this.setData({
      isLoadingBusy: true,
      discountCouponList: [],
      adminDiscountCouponList: []
    })
    const that = this
    common.post('/cityDisCoupon/getCityDisCouponListByRegionId', tempJson, function (res) {
      if (res.status === 200) {
        that.setData({
          discountCouponList: res.data.discountCouponList,
          adminDiscountCouponList: res.data.adminDiscountCouponList
        })
      } else {
        common.showRequestErrMsg(res)
      }
    }, function () {
      that.setData({
        isLoadingBusy: false
      })
    })
  },
  selectCity(e) {
    const that = this
    const {
      index,
      item
    } = e.currentTarget.dataset
    if (this.data.activeCity == index) {
      // 点击当前市，不需要重新刷新数据
      return
    }
    // 切换到当前城市，默认显示的是地级市，没有区县id，这里将区县id值为空
    this.data.regionId = ''
    this.setData({
      activeCity: index,
      activeRegion: -1
    }, function () {
      that.getAllDisTrict() // 获取区县
      that.changeImageId() // 更新城市id
      that.getCityCoupons(item.v)
    })
  },
  // getCityDisCouponList() { // 获得市券列表
  //   wx.showLoading({
  //     title: '加载中...',
  //   })
  //   const that = this
  //   let parames = that.data.parames
  //   let tempJson = {
  //     ...parames
  //   }
  //   common.post('/cityDisCoupon/getCityDisCouponList', tempJson, function (res) {
  //     if (res.status === 200) {
  //       if (tempJson.couponType) { // 筛选条件
  //         console.log('tempJson', tempJson.couponType, res.data)
  //         that.setData({
  //           discountCouponList: res.data,
  //           discountTypeList: res.data.discountTypeList,
  //         }, function () {
  //           wx.hideLoading()
  //         })
  //       } else { //全部
  //         let newDiscountTypeList = []
  //         let newAdminDiscountTypeList = []
  //         res.data.discountTypeList.map((v, i) => {
  //           newDiscountTypeList.push({
  //             ...v,
  //             type: 1
  //           })
  //         })
  //         res.data.adminDiscountTypeList.map((v, i) => {
  //           newAdminDiscountTypeList.push({
  //             ...v,
  //             type: 2
  //           })
  //         })
  //         // console.log('newDiscountTypeList',newDiscountTypeList)
  //         that.setData({
  //           discountCouponList: res.data.discountCouponList,
  //           discountTypeList: [...newDiscountTypeList, ...newAdminDiscountTypeList],
  //           adminDiscountCouponList: res.data.adminDiscountCouponList,
  //           adminDiscountTypeList: res.data.adminDiscountTypeList
  //         })
  //       }
  //     } else {
  //       common.showRequestErrMsg(res)
  //     }
  //   }, function () {
  //     wx.hideLoading()
  //   })
  // },
  // getAdminCouponListByCondition() { // 获得商家券列表
  //   let that = this
  //   let parames = that.data.parames
  //   let tempJson = {
  //     ...parames
  //   }
  //   common.post('/discountCoupon/getAdminCouponListByCondition', tempJson, function (res) {
  //     console.log(res)
  //     that.setData({
  //       adminDiscountCouponList: res.data
  //     })
  //   })
  // },
  selectRegion(e) { // 点击上方区县选项卡
    console.log(e,"点击事件666")
    let {
      index,
      item
    } = e.currentTarget.dataset
    if (this.data.activeRegion == index) {
      return
    }
    const regionId = (item && item.v) ? item.v : ''
    console.log(regionId)
    this.setData({
      activeRegion: index,
      regionId,
      managerId:item.managerId ? item.managerId : null
    })
    // 这里需要真的区县处理图片id
    // if (regionId && utils.containRegion(regionId)) {
    if (regionId==1851 || regionId==1849) {
      this.setData({
        imageId: regionId
      })
    } else {
      this.setData({
        imageId: this.data.citys[this.data.activeCity].v
      })
    }
    if (index == -1) { // 全部
      this.getCityCoupons(this.data.citys[this.data.activeCity].v)
    } else {
     
      this.getDisTrictCoupons(item.v)
    }
  },
  toCultureCouponsDetail(e) { // 跳转惠民券详情页
    let dcId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/cultureCoupons/cultureCouponsDetail/index?dcId=' + dcId,
    })
  },
  toPlatformCoupons(e) {
    let dcId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/cultureCoupons/platformCoupons/index?dcId=' + dcId,
    })
  },
  /**
   * 刷新优惠券数据
   */
  reloadCouponData() {
    if (this.data.activeCity == -1) {
      // 当前是省券列表
      this.getProvinceCoupons()
    } else {
      if (this.data.activeRegion == -1) {
        // 当前是市券列表
        const city = this.data.citys[this.data.activeCity]
        if (city) {
          this.getCityCoupons(city.v)
        }
      } else {
        // 当前是区域列表
        this.getDisTrictCoupons(this.data.regionId)
      }
    }
  },
  getUserInfo(){
    this.setData({
      
    })
  },
  /**
   * 跳转到消费券专题页面
   */
  toCouponsTopicPage() {
    
    // console.log("跳转669993333")
    let provinceId = ''
    let cityId = ''
    let regionId = ''
    // if (this.data.activeCity == -1) {       //山东省
    //    provinceId = 16
    //    山东省活动
    //    if (provinceId === 16) {
    //     let tmpList = []
    //     !common.isEmptyString(provinceId) && tmpList.push('provinceId=' + provinceId)
    //     !common.isEmptyString(cityId) && tmpList.push('cityId=' + cityId)
    //     !common.isEmptyString(regionId) && tmpList.push('regionId=' + regionId)
    //     const url = '/pages/cultureCoupons/couponsTopicActivePage/index?' + tmpList.join('&')
    //     console.log(url)
    //     // 跳转携带参数
    //     wx.navigateTo({
    //       url
    //     })
    //     return
    //   }
    // } else {
    //   const city = this.data.citys[this.data.activeCity]
    //   regionId = this.data.regionId  
    //   cityId = city.v
    //   if(cityId === 173 || cityId === 174 || cityId === 175 || cityId === 179 || cityId === 180  || cityId === 182  ||cityId === 184||cityId === 185||cityId === 186){  //枣庄
    //     let tmpList = []
    //     !common.isEmptyString(provinceId) && tmpList.push('provinceId=' + provinceId)
    //     !common.isEmptyString(cityId) && tmpList.push('cityId=' + cityId)
    //     !common.isEmptyString(regionId) && tmpList.push('regionId=' + regionId)
    //     const url = '/pages/cultureCoupons/couponsTopicActivePage/index?' + tmpList.join('&')
    //     console.log(url)
    //     console.log(tmpList)
    //     // 跳转携带参数
    //     wx.navigateTo({
    //       url
    //     })
    //     return
    //   }
    // }
    // 新改的跳转路径
    const city = this.data.citys[this.data.activeCity]
    regionId = this.data.regionId  
    cityId = city.v
    // console.log(city,"选中的城市")
    // console.log(regionId,"regionIdregionId")
    // regionId = this.data.regionId  
    if(!regionId){
      this.setData({
        managerId:city.managerId
      })
    }
    // 判断是否选中其他的地级市
    let tmpList = []
    !common.isEmptyString(provinceId) && tmpList.push('provinceId=' + provinceId)
    !common.isEmptyString(cityId) && tmpList.push('cityId=' + cityId)
    !common.isEmptyString(regionId) && tmpList.push('regionId=' + regionId)
    !common.isEmptyString(this.data.cityes) && tmpList.push('cityes=' + this.data.cityes)
    var url = '/pages/Dj/index?' + tmpList.join('&')
    // 跳转携带参数
    // 在这判断是否携带mannagerid
   if(this.data.managerId){
    var url = '/packageB/pages/DjM/index?managerId=' +this.data.managerId
   }
  // console.log(this.data.managerId)
    wx.navigateTo({
      url
    })

    // 原来的路径
    // let tmpList = []
    // !common.isEmptyString(provinceId) && tmpList.push('provinceId=' + provinceId)
    // !common.isEmptyString(cityId) && tmpList.push('cityId=' + cityId)
    // !common.isEmptyString(regionId) && tmpList.push('regionId=' + regionId)
    // const url = '/pages/cultureCoupons/couponsTopicPage/index?' + tmpList.join('&')
    // console.log(url)
    // // 跳转携带参数
    // wx.navigateTo({
    //   url
    // })

  },
  changeImageId() {
    // 图片id和当前城市的value值相同
    console.log("11111")
    console.log(this.data.activeCity)
    if (this.data.activeCity === -1) {  //-1是山东省
      this.setData({
        imageId: '16'
      })
      return
    }
  
    const city = this.data.citys[this.data.activeCity]
    
    this.setData({
        cityes:city.l
    })
    if (!city) {
    
      this.setData({
        imageId: 'default'
      })
      return
    }
    const imageId = city.v ? city.v : 'default'
    this.setData({
      imageId
    })
  },
  onShareAppMessage: function () {
    return {
      title: '第六届文旅惠民消费季',
      path: '/pages/cultureCoupons/index',
      imageUrl: 'https://feiyangimage.oss-cn-shanghai.aliyuncs.com/wxApplets/xfj/share.jpg',
      success: (res) => { },
      fail: (res) => {
        // 分享失败
      }
    }
  },
  onShareTimeline() {
    const query = ''
    return {
      title: '第六届文旅惠民消费季',
      query,
      imageUrl: 'https://feiyangimage.oss-cn-shanghai.aliyuncs.com/wxApplets/xfj/share.jpg'
    }
  },
})